/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.app.client.form.bindings;

import java.io.Serializable;

import org.nuxeo.gwt.habyt.app.client.form.FormControl;
import org.nuxeo.gwt.habyt.templates.client.bindings.Helper;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.SimpleCheckBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public class CheckBoxFactory extends AbstractControlFactory {

    public CheckBoxFactory() {
        super("checkbox");
    }

    @Override
    protected FormControl createControl(Element element) {
        String name = Helper.getAttribute(element, "name");
        String disabled = Helper.getAttribute(element, "disabled");
        String checked = Helper.getAttribute(element, "checked");
        String label = element.getInnerHTML().trim();
        Widget w = null;
        if (label.length() > 0) {
            CheckBox cb = new CheckBox(label, true);
            w = cb;
            if (name != null) {
                cb.setName(name);
            }
            if (checked != null) {
                cb.setValue(Boolean.parseBoolean(checked));
            }
            if (disabled != null) {
                cb.setEnabled(!Boolean.parseBoolean(disabled));
            }
        } else {
            SimpleCheckBox cb = new SimpleCheckBox();
            w = cb;
            if (name != null) {
                cb.setName(name);
            }
            if (checked != null) {
                cb.setChecked(Boolean.parseBoolean(checked));
            }
            if (disabled != null) {
                cb.setEnabled(!Boolean.parseBoolean(disabled));
            }
        }
        return new FormControl(w) {

            @Override
            public Serializable getValue() {
                if (getWidget() instanceof SimpleCheckBox) {
                    return ((SimpleCheckBox) getWidget()).isChecked();
                } else {
                    return ((CheckBox) getWidget()).getValue();
                }
            }

            public void setValue(Serializable value) {
                Boolean v = null;
                if (value instanceof String) {
                    v = Boolean.parseBoolean((String) value);
                } else if (value instanceof Boolean) {
                    v = (Boolean) value;
                } else if (value != null) {
                    throw new IllegalArgumentException(
                            "Illegal value type for check box: " + value);
                } else {
                    return;
                }
                if (getWidget() instanceof SimpleCheckBox) {
                    ((SimpleCheckBox) getWidget()).setChecked(v);
                } else {
                    ((CheckBox) getWidget()).setValue(v);
                }
            }
        };
    }

}
